﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XrmLibrary.MsCrm4Adapter.Extensions
{
    public static class RolePrivilegeExtensions
    {
        public static Microsoft.Crm.Sdk.RolePrivilege ToCrm4RolePrivilege(this Microsoft.Crm.Sdk.Messages.RolePrivilege crm2011RolePrivilege)
        {
            Microsoft.Crm.Sdk.RolePrivilege crm4RolePrivilege =
                new Microsoft.Crm.Sdk.RolePrivilege()
                {
                    Depth = crm2011RolePrivilege.Depth.ToCrm4PrivilegeDepth(),
                    PrivilegeId = crm2011RolePrivilege.PrivilegeId
                };

            return crm4RolePrivilege;
        }

        public static IEnumerable<Microsoft.Crm.Sdk.RolePrivilege> ToCrm4RolePrivilegeCollection(this Microsoft.Crm.Sdk.Messages.RolePrivilege[] crm2011RolePrivilege)
        {
            foreach (Microsoft.Crm.Sdk.Messages.RolePrivilege rolePrivilege in crm2011RolePrivilege)
            {
                yield return rolePrivilege.ToCrm4RolePrivilege();
            }
        }

        public static Microsoft.Crm.Sdk.Messages.RolePrivilege ToCrm2011RolePrivilege(this Microsoft.Crm.Sdk.RolePrivilege crm4RolePrivilege)
        {
            Microsoft.Crm.Sdk.Messages.RolePrivilege crm2011RolePrivilege =
                new Microsoft.Crm.Sdk.Messages.RolePrivilege
                {
                    BusinessUnitId = Guid.Empty,
                    Depth = crm4RolePrivilege.Depth.ToCrm2011PrivilegeDepth(),
                    PrivilegeId = crm4RolePrivilege.PrivilegeId
                };

            return crm2011RolePrivilege;
        }

        public static IEnumerable<Microsoft.Crm.Sdk.Messages.RolePrivilege> ToCrm2011RolePrivilegeCollection(this Microsoft.Crm.Sdk.RolePrivilege[] crm4RolePrivilege)
        {
            foreach (Microsoft.Crm.Sdk.RolePrivilege rolePrivilege in crm4RolePrivilege)
            {
                yield return rolePrivilege.ToCrm2011RolePrivilege();
            }
        }
    }
}
